public class SortAuditClass{
/**********************************************************************
 Name:  SortAuditClass
 Purpose:use for sort audit plan, sort the record by javascript, this controller just use for save the results                                
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR            DATE              DETAIL                                
   1.0         li siheng         2011-4-23        Create          
***********************************************************************/
    
    public String[] SortValue{get;set;}
    
    public Date AuditDate{get;set;}
    
    public list<Audit__c> AuditList{get;set;}
    
    public List<SelectOption> OptionList{get;set;}
    
    public map<id,audit__c> AuditMap{get;set;}
 
    
    public PageReference save(){
        list<audit__c> updateAuditList=new list<audit__c>();
        //按照sortvalue排序
        for(integer i=0;i<SortValue.size();i++){
            Audit__c audit=new Audit__c();
            audit=auditmap.get(SortValue[i]);
            audit.sort__c=i+1;
            updateAuditList.add(audit);
        }
        update updateauditlist;
        pageReference pr = new pageReference('/apex/AuditPlanPage'); 
        pr.setredirect(true);
        return pr;
    }

    public PageReference cancel(){
        pageReference pr = new pageReference('/apex/AuditPlanPage'); 
        pr.setredirect(true);
        return pr;
    }
    
    public SortAuditClass(ApexPages.StandardController controller) {
        this.controller = controller;
        try{
        AuditDate=date.valueof(System.currentPageReference().getParameters().get('date'));
        }catch(exception e){

            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, '错误，缺少拜访日期，请返回并重新操作，如有疑问请联系管理员。')); 
        }
        //system.assert(auditdate != null,'错误，缺少拜访日期，请返回并重新操作，如有疑问请联系管理员。');
        SortValue=new String[]{}; 
        AuditList=new list<Audit__c>();
        AuditMap=new map<id,audit__c>();
        AuditList=[select id,Outlet__c from Audit__c where Plan_Date__c =:auditdate and Owner__c=: userinfo.getuserid() order by sort__c];
        for(Audit__c a:auditList){
            auditmap.put(a.id,a);
        }
        if(OptionList==null)
            OptionList= new List<SelectOption>();
           
        for(Audit__c a:AuditList){
            OptionList.add(new SelectOption(a.id,a.Outlet__c+''));
        }
    }    

    private final ApexPages.StandardController controller;
    static testMethod  void test()
    {
        Profile p = [select id from profile where name='System Administrator' or name = '系统管理员' limit 1];
        User u2 = new User(alias = 'standtss', email='sdardr@testorgss.com',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',Title__c='TSR',
        localesidkey='en_US', profileid = p.Id,UserCity__c='广州',IsActive=true,
        UserAreaValue__c='广州区域',UserSubRegion__c='华南一区',UserRegion__c='华南区',
        timezonesidkey='America/Los_Angeles', username='stsers@testorg.com');
        insert u2;  
        
        
        
        system.runas(u2){
            user u=[select id from user where Title__c = 'Sales Rep' and FunctionDepartment__c='Trade Sales' and isactive=true limit 1];
            Routing_Plan_And_Visit__c RPAV=new Routing_Plan_And_Visit__c(Visit_Date__c=date.today());
            insert rpav;
            in_store_visit__c isv=new in_store_visit__c();
            isv.LabelIsInTheCorrectPlace__c=true;
            isv.MainShelfDisplayIsStandard__c=true;
            isv.Main_Shelf_Facing__c=23;
            isv.End_Time__c=datetime.now();
            isv.Start_Time__c=datetime.now();
            isv.RoutingPlanAndVisit__c=rpav.id;
            isv.OutletName_del__c=[select id from account limit 1].id;
            insert isv;
            audit__c audit=new audit__c();
            audit.Plan_Date__c=date.today();
            audit.in_store_visit__c=isv.id;
            audit.owner__c=u2.id;
            insert audit;
            
            Audit__c a=new Audit__c();
            ApexPages.StandardController controller=new ApexPages.StandardController(a);
            SortAuditClass auditClass=new SortAuditClass(controller);
           
            auditclass.auditmap.put(audit.id,audit);
            auditclass.sortvalue.add(audit.id);
            auditClass.save();
            
            //test SyncAuditToCalendar trigger delete condition
            delete audit;
        }
        
    }
}